# Create your models here.

import os
import uuid
import time
import random
import datetime
from django.db import models
from django.forms import ModelChoiceField
from django.contrib.auth.models import User


def modify_path(instance, filename):
    '''
    重定义图片保存路径
    :param instance: self
    :param filename: 文件名
    :return: 新路径
    '''
    #print(filename) #r7.png
    ext = filename.split('.').pop() #png
    now_date = datetime.datetime.now().strftime('%Y%m%d')
    now_time = str(int(time.time()))+str(random.randint(100,999))
    filename = '{0}.{1}'.format( now_time, ext)
    return os.path.join('cover_img', now_date, filename)  # 系统路径分隔符差异，增强代码重用性


##############################################################表 业务

##归属分类
###公司现场项目分类管理
class organ_index_company(models.Model):
    company = models.CharField(max_length=255, null=True, blank=True, unique=True,verbose_name='公司', db_comment='公司名字')
    comp_addr = models.CharField(max_length=255, verbose_name='公司地址', db_comment='公司名字')
    main_user1 = models.CharField(max_length=255, verbose_name='主联系人', db_comment='联系人')
    main_user2 = models.CharField(max_length=255, verbose_name='备联系人', db_comment='联系人')
    func = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简介', db_comment="xx简介")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='era', null=True, blank=True,verbose_name='导入人', db_comment='导入人')
    status = models.BooleanField(default=True, verbose_name='是否删除', db_comment='标记是否已删除，0代表删除')
    class Meta:
        verbose_name = '公司'
        verbose_name_plural = '归属分类'
    def __str__(self):
        return str(self.company)
class organ_index_scene(models.Model):
    scene = models.CharField(max_length=255,verbose_name='现场',unique=True,db_comment='现场名字')
    func = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简介', db_comment="xx简介")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='awq', null=True, blank=True,verbose_name='创建人', db_comment='创建人')
    status = models.BooleanField(default=True, verbose_name='是否删除', db_comment='标记是否已删除，0代表删除')
    class Meta:
        verbose_name = '现场'
        verbose_name_plural = '归属分类'
    def __str__(self):
        return str(self.scene)
class organ_index_project(models.Model):
    project = models.CharField(max_length=255,unique=True,verbose_name='项目',db_comment='具体子项、功能、系统的名字')
    func = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简介', db_comment="xx简介")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='ser', null=True, blank=True,verbose_name='创建人', db_comment='创建人')
    status = models.BooleanField(default=True, verbose_name='是否删除', db_comment='标记是否已删除，0代表删除')
    class Meta:
        verbose_name = '项目'
        verbose_name_plural = '归属分类'
    def __str__(self):
        return str(self.project)
class organ_index_class(models.Model):
    device_class = models.CharField(max_length=255,unique=True,null=True, blank=True,verbose_name='类型',db_comment='设备类型')
    func = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简介', db_comment="xx简介")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='ada', null=True, blank=True,verbose_name='创建人', db_comment='创建人')
    status = models.BooleanField(default=True, verbose_name='是否删除', db_comment='标记是否已删除，0代表删除')
    class Meta:
        verbose_name = '类型'
        verbose_name_plural = '归属分类'
    def __str__(self):
        return str(self.device_class)


#设备归属表
class Device_info(models.Model):
    company =models.ForeignKey(organ_index_company,on_delete=models.SET_NULL,related_name='fre', null=True, blank=True, verbose_name='公司', db_comment='关联公司')
    scene = models.ForeignKey(organ_index_scene,on_delete=models.SET_NULL,related_name='dewe', null=True, blank=True, verbose_name='现场', db_comment='关联现场名字')
    project = models.ForeignKey(organ_index_project,on_delete=models.SET_NULL,related_name='hyh', null=True, blank=True, verbose_name='项目', db_comment='关联具体子项、功能、系统的名字')
    device_class  =models.ForeignKey(organ_index_class,on_delete=models.SET_NULL,related_name='vbe', null=True, blank=True, verbose_name='类型', db_comment='关联设备类型')
    device_name = models.CharField(max_length=255, verbose_name='名称', db_comment='设备名称')
    device_id = models.CharField(max_length=32, verbose_name='唯一编号',unique=True,db_comment='设备的uuid唯一标识')
    admin_user =models.ForeignKey(User, on_delete = models.SET_NULL,null=True, blank=True,verbose_name='管理员',db_comment='技术管理员、责任人')
    teach_user = models.ForeignKey(User, on_delete = models.SET_NULL,related_name='qw',null=True, blank=True,verbose_name='技术员', db_comment='技术人')
    watch_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='ty', null=True, blank=True,verbose_name='操作员', db_comment='现场值班、操作员')
    create_user = models.ForeignKey(User, on_delete = models.SET_NULL,related_name='er',null=True, blank=True,verbose_name='创建人',db_comment='创建人')
    func = models.TextField(max_length=1000,null=True, blank=True, verbose_name='功能描述',db_comment="设备功能简介")
    photo = models.ImageField('照片', upload_to=modify_path, null=True, blank=True, default='/cover_img/default.jpg')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间',db_comment='创建时间')
    status = models.BooleanField(default=True,verbose_name='是否删除',db_comment='标记是否已删除，0代表删除')

    class Meta:
        verbose_name = '设备归属' #WEB端 表名映射为中文
        verbose_name_plural ='设备管理'
    #not
    # 根据原始字段的值生成新的字段值
    #def save(self, *args, **kwargs):
        #self.rname =  self.device_name+'_'+self.device_id
        #super(Device_info, self).save(*args, **kwargs)

    # Device_attribute 表需要调用
    def __str__(self):
        #return str(str(self.company.company)+'/'+str(self.scene.scene)+'/'+str(self.project.project)+'/'+self.device_name)
        return str(str(self.company )+'/'+str(self.scene )+'/'+str(self.project )+'/'+self.device_name)
        # ForeignKey字段可使用self.company.company引用 organ_index_company的company键值

    #可在其他models 用i.device_name.abc()访问
    def abc(self):
        return str(str(self.company.company) + '/' + str(self.scene.scene) + '/' + str(
            self.project.project) + '/' + self.device_name)+'abc'
#设备详情属性功能详情表
class Device_attribute(models.Model):
    device_name =models.ForeignKey(Device_info,to_field='device_id',on_delete=models.SET_NULL,related_name='awi', null=True, blank=True, verbose_name='设备', db_comment='关联设备id')
    a1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='A', db_comment='自定义')
    b1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='B', db_comment='自定义')
    c1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='C', db_comment='自定义')
    d1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='D', db_comment='自定义')
    e1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='E', db_comment='自定义')
    f1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='F', db_comment='自定义')
    g1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='G', db_comment='自定义')
    h1 = models.CharField(max_length=255,null=True, blank=True, verbose_name='H', db_comment='自定义')
    notes = models.TextField(max_length=1000,null=True, blank=True, verbose_name='备注',db_comment="备注")
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='es', null=True, blank=True,verbose_name='创建人', db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True,null=True, blank=True,verbose_name='创建时间',db_comment='创建时间')
    order_id = models.SmallIntegerField(null=True, blank=True, verbose_name='排序', db_comment='手动指定排序优先级')
    is_head = models.BooleanField(default=True, verbose_name='是否表头', db_comment='表头')
    status = models.BooleanField(default=True,verbose_name='是否删除',db_comment='标记是否已删除，0代表删除')

    class Meta:
        verbose_name = '设备详情' #WEB端 表名映射为中文
        verbose_name_plural = '设备管理'


#设备维修记录
class Device_maintenance_log(models.Model):
    device_name = models.ForeignKey(Device_info, to_field='device_id', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='设备', db_comment='设备ID设备的uuid唯一标识')
    maintenance_user = models.ForeignKey(User, on_delete = models.SET_NULL,related_name='fg',null=True, blank=True, verbose_name='维修人', db_comment='维修人')
    replace_ch = [(0, '是'),(1, '否'),]
    replace = models.IntegerField(choices=replace_ch,verbose_name='更换', db_comment='标记是否更换')
    problem= models.CharField(max_length=255,null=True, blank=True, verbose_name='故障详情', db_comment='故障说明')
    solution=models.CharField(max_length=255,null=True, blank=True, verbose_name='解决方案', db_comment='解决方案')
    notes = models.TextField(max_length=1000, null=True, blank=True, verbose_name='备注', db_comment="备注")
    photo1 = models.ImageField('照片', upload_to=modify_path, null=True, blank=True, default='/cover_img/default.jpg')
    photo2 = models.ImageField('照片', upload_to=modify_path, null=True, blank=True, default='/cover_img/default.jpg')
    photo3 = models.ImageField('照片', upload_to=modify_path, null=True, blank=True, default='/cover_img/default.jpg')
    plan_ch = [('等待处理', '等待处理'),('正在进行', '正在进行'),('维修完成', '维修完成'),]

    plan  = models.CharField(max_length=255,choices=plan_ch,null=True, blank=True,verbose_name='进度', db_comment='进度')
    #故障报告
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='时间',db_comment='时间')
    status = models.BooleanField(default=True, verbose_name='标记是否已删除，0代表删除')

    class Meta:
        verbose_name = '维修记录'
        verbose_name_plural ='设备管理'


#标签管理
class Device_lable(Device_info):
    class Meta:
        verbose_name = "标签查看"
        verbose_name_plural ='标签管理'
        proxy = True


#登录日志
class Loginlog(models.Model):
    user = models.CharField(max_length=255,null=True, blank=True, verbose_name='用户', db_comment='用户')
    pcname = models.CharField(max_length=255,null=True, blank=True, verbose_name='主机名称', db_comment='主机名称')
    os = models.CharField(max_length=255,null=True, blank=True, verbose_name='操作系统', db_comment='操作系统')
    remot = models.CharField(max_length=255,default='-',null=True, blank=True, verbose_name='代理', db_comment='代理')
    addr = models.CharField(max_length=255,null=True, blank=True, verbose_name='IP地址', db_comment='IP地址')
    ua = models.CharField(max_length=255,null=True, blank=True, verbose_name='ua', db_comment='ua')
    ag = models.CharField(max_length=255,null=True, blank=True, verbose_name='ag', db_comment='ag')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='登录时间', db_comment='登录时间')
    status = models.BooleanField(default=True, verbose_name='标记是否已删除，0代表删除')

    class Meta:
        verbose_name = '登录日志'
        verbose_name_plural ='日志管理'


# 录入统计 记录每个人的录入数据量
class Enter_times(models.Model):
    user =  models.ForeignKey(User, on_delete=models.SET_NULL, related_name='fsj', null=True, blank=True, verbose_name='创建人',db_comment='创建人')
    times = models.CharField(max_length=255,null=True, blank=True, verbose_name='录入数量', db_comment='录入数量')
    proportion = models.CharField(max_length=255, null=True, blank=True, verbose_name='对比', db_comment='对比占比')
    create_time = create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')
    class Meta:
        verbose_name = '录入统计'
        verbose_name_plural = '录入报表'

# 维修报表
class maintenance_times(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='wqe', null=True, blank=True,
                             verbose_name='维修人', db_comment='维修人')
    times = models.CharField(max_length=255, null=True, blank=True, verbose_name='维修数量', db_comment='维修数量')
    proportion = models.CharField(max_length=255, null=True, blank=True, verbose_name='对比', db_comment='对比占比')
    create_time = create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '维修报表'
        verbose_name_plural = '报表报表'

# iot
'''
    #设备表
class iot_device(models.Model):
    device = models.FloatField(null=True, blank=True, verbose_name='设备名称', db_comment='设备名称')
    # device_id

    # iot状态表
class iot_status(models.Model):
    device = models.FloatField(null=True, blank=True, verbose_name='设备名称', db_comment='设备名称')
    #device_id
    tag = models.CharField(max_length=255, null=True, blank=True, verbose_name='标签', db_comment='标签')
    online = models.BooleanField(default=True, verbose_name='在线', db_comment='在线离线')
    status = models.BooleanField(default=True, verbose_name='状态', db_comment='正常异常')
    info = models.CharField(max_length=255, null=True, blank=True, verbose_name='对比', db_comment='对比占比')
    uptime = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '状态表'
        verbose_name_plural = '状态表'
'''
    #电表数据
class electricity_meter(models.Model):
    u = models.FloatField(null=True, blank=True, verbose_name='电压', db_comment='电压值')
    i = models.FloatField(null=True, blank=True, verbose_name='电流', db_comment='电流值')
    q = models.FloatField(null=True, blank=True, verbose_name='功率因素', db_comment='功率因素')
    w = models.FloatField(null=True, blank=True, verbose_name='用电量', db_comment='用电量')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '电表'
        verbose_name_plural = '电表'
    #温度数据
class Temperature_humidity(models.Model):
    temperature = models.FloatField(null=True, blank=True, verbose_name='温度', db_comment='温度')
    humidity = models.FloatField(null=True, blank=True, verbose_name='湿度', db_comment='湿度')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '温湿度表'
        verbose_name_plural = '温湿度表'




#report-每日故障
class Alex_day_maintenance(models.Model):
    device_name=models.ForeignKey(Device_info, to_field='device_id',on_delete=models.SET_NULL,related_name='awwq', null=True, blank=True, verbose_name='设备名称', db_comment='test_id')
    times =models.CharField(max_length=255,null=True, blank=True, verbose_name='故障次数', db_comment='自定义')
    date = models.DateField(null=True, blank=True,verbose_name='日期', db_comment='日期')
    create_time = create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')
    class Meta:
        verbose_name = '每日故障'
        verbose_name_plural = '故障报表'
#report-每月故障
class Alex_mouth_maintenance(models.Model):
    device_name=models.ForeignKey(Device_info, to_field='device_id',on_delete=models.SET_NULL,related_name='awser', null=True, blank=True, verbose_name='设备名称', db_comment='test_id')
    times =models.CharField(max_length=255,null=True, blank=True, verbose_name='故障次数', db_comment='自定义')
    create_time = create_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间', db_comment='更新时间')
    class Meta:
        verbose_name = '每月故障'
        verbose_name_plural = '故障报表'
#月度耗材统计
class consumable_materials(models.Model):
    class Meta:
        verbose_name = '耗材统计'
        verbose_name_plural = '耗材报表'
#docx 文档
class doc(models.Model):
    pdf_file = models.FileField(upload_to='pdfs/',verbose_name='文件名称', db_comment='文件名称')
    notes = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简要说明', db_comment="简要说明")
    create_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='fgj', null=True, blank=True, verbose_name='创建人',db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True,null=True, blank=True, verbose_name='更新时间', db_comment='更新时间')
    def __str__(self):
        return str(self.pdf_file)
'''
#demos
class Device_attribute(models.Model):
    te = models.JSONField(null=True, blank=True,verbose_name='JSTEST',db_comment='测试json')
    
    cover = models.ImageField('照片', upload_to=modify_path, null=True, blank=True, default='/cover_img/default.jpg')
        # upload_to可指定路径，如upload_to='cover_img',图片上传后将保存在 static/media/cover_img/原文件名。这里我们自行生成保存路径
    
    # 这里定义一个方法，作用是当用户注册时没有上传照片，模板中调用 [ModelName].[ImageFieldName].url 时赋予一个默认路径
    def photo_url(self):
        if self.photo and hasattr(self.photo, 'url'):
            return self.photo.url
        else:
            return '/media/default/user.jpg'

'''




#test demo
class testmod(models.Model):
    name = models.TextField()
    file = models.TextField(null=True, blank=True, )
    date = models.DateTimeField(auto_now_add=True,null=True, blank=True, )
#cmd
'''
python manage.py makemigrations 
python manage.py migrate  serverworker
'''